package net.hz.utils;

import java.io.IOException;
import java.util.Map;

import org.jsoup.connect.Connection;
import org.jsoup.connect.Connection.Method;
import org.jsoup.connect.Connection.Response;
import org.jsoup.helper.HttpConnection;

import org.jsoup.nodes.Document;

public abstract class HttpLoad {
	private final static String PROXY_HOST = "internet2.can.eds.com";
	private final static int PROXY_PORT = 8080;
	private final static String PROXY_UID = "siegleda";
	private final static String PROXY_PWD = "Calgary99!1";
	
//	public static boolean useProxy = true;
	public static boolean useProxy = false;
	public static Map<String, String> login() throws IOException {
		try {
			Connection jsConn = HttpConnection.connect("http://hacker-project.com/index.php");
			
			if (useProxy)
				((HttpConnection) jsConn).proxy(PROXY_HOST, PROXY_PORT, PROXY_UID, PROXY_PWD);
			
			Response res = jsConn.timeout(12000)
			 .userAgent("Mozilla")
			 .data("action", "login", "user", "CapnCrunch", "pwd", "calgary1")
			 .method(Method.POST)
			 .execute();
			
			return res.cookies();
		} catch (IOException e) {
			throw e;
		}
	}
	
	public static Response postPage(String url, Map<String, String> cookies, Map<String, String> postData) throws IOException {
		try {
			Connection jsConn = HttpConnection.connect(url);
			
			if (useProxy)
				((HttpConnection) jsConn).proxy(PROXY_HOST, PROXY_PORT, PROXY_UID, PROXY_PWD);
			
			jsConn.timeout(12000)
			 .userAgent("Mozilla")
			 .cookies(cookies)
			 .data(postData)
			 .method(Method.POST);
			
			return jsConn.execute();
		} catch (IOException e) {
			throw e;
		}
	}
	
	public static Document postPage(String url, Map<String, String> cookies, String... keyvals) throws IOException {
		try {
			Connection jsConn = HttpConnection.connect(url);
			
			if (useProxy)
				((HttpConnection) jsConn).proxy(PROXY_HOST, PROXY_PORT, PROXY_UID, PROXY_PWD);
			
			jsConn.timeout(12000)
			 .userAgent("Mozilla")
			 .cookies(cookies)
			 .data(keyvals)
			 .method(Method.POST);
			
			return jsConn.post();
		} catch (IOException e) {
			throw e;
		}
	}
	
	public static Document getDocument(String url, Map<String, String> cookies, String... keyvals) throws IOException {
		try {

			Connection jsConn = HttpConnection.connect(url);
			
			if (useProxy)
				((HttpConnection) jsConn).proxy(PROXY_HOST, PROXY_PORT, PROXY_UID, PROXY_PWD);
			
			jsConn.timeout(6000);
			jsConn.userAgent("Mozilla");
			jsConn.data(keyvals);
			jsConn.cookies(cookies);
			
			return jsConn.get();
		} catch (IOException e) {
			throw e;
		}
	}
}
